package cn.longwingstech.intelligence.agent.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.longwingstech.intelligence.agent.aop.RateLimiter;
import cn.longwingstech.intelligence.agent.common.BaseResponse;
import cn.longwingstech.intelligence.agent.common.ResultUtils;
import cn.longwingstech.intelligence.agent.model.dto.user.UserLoginRequest;
import cn.longwingstech.intelligence.agent.model.dto.user.UserRegisterRequest;
import cn.longwingstech.intelligence.agent.model.dto.user.UserUpdateRequest;
import cn.longwingstech.intelligence.agent.model.vo.user.UserLoginVO;
import cn.longwingstech.intelligence.agent.model.vo.user.UserVO;
import cn.longwingstech.intelligence.agent.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@Tag(name = "用户认证控制层")
public class AuthController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     * @param userLoginRequest
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public BaseResponse<UserLoginVO> login(@RequestBody UserLoginRequest userLoginRequest) {
        UserLoginVO result = userService.login(userLoginRequest);
        return ResultUtils.success(result);

    }

    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public BaseResponse<Boolean> register(@RequestBody UserRegisterRequest userRegisterRequest) {
        Long result = userService.register(userRegisterRequest);
        return ResultUtils.success(true);
    }

    @PostMapping("/update")
    @Operation(summary = "用户更新")
    public BaseResponse<Boolean> update(@RequestBody UserUpdateRequest userUpdateRequest) {
        Boolean result = userService.usersUpdate(userUpdateRequest);

        return ResultUtils.success(true);
    }

    /**
     * 用户退出
     * @return
     */
    @SaCheckLogin
    @PostMapping("/logout")
    @Operation(summary = "用户退出")
    public BaseResponse<Boolean> logout() {
        StpUtil.logout();
        return ResultUtils.success(true);
    }

    /**
     * 获取当前用户信息
     * @return
     */
    @SaCheckLogin
    @GetMapping("/currentUser")
    @Operation(summary = "获取当前用户信息")
    public BaseResponse<UserVO> currentUser() {
        UserVO result =userService.getCurrentUser();
        return ResultUtils.success(result);
    }

    /**
     * 获取验证码
     * @return
     */
    @GetMapping("/getCode")
    @Operation(summary = "获取验证码")
    @RateLimiter(key = "getCode", count = 5)
    public BaseResponse<Boolean> getCode(@RequestParam("email") String email){
        Boolean result = userService.getCode(email);
        return ResultUtils.success(result);
    }
}
